//
// @Author: Zhenwei Xiao
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/2 下午4:12
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const identifier = require("../component/Identifier")
const identifiercomponent = require("../component/IdentifierComponent")
const ControlParameters = require("../component/ControlParameters")
const encoding = require("../encoding/Encoder")
const inst = require("../packet/Interest")
const {createIdentifierByComponents} = require("../component/Identifier");
//
// @Description: 	创建注册标识的兴趣包
// @param identifier *component.Identifier
// @return *packet.Interest, error
//
function createRegisterIdentifierInterest(identifier) {
    // /min-mir/mgmt/localhop/<模块名称>/<命令>/<参数>/[版本号]/[分片号]
    var interest = new inst.Interest
    var componentContainer = new Array()
    componentContainer.push(identifiercomponent.createIdentifierComponentByString("min-mir"))
    componentContainer.push(identifiercomponent.createIdentifierComponentByString("mgmt"))
    componentContainer.push(identifiercomponent.createIdentifierComponentByString("localhop"))
    componentContainer.push(identifiercomponent.createIdentifierComponentByString("fib-mgmt"))
    componentContainer.push(identifiercomponent.createIdentifierComponentByString("add-next-hop"))

    var controlParameters = new ControlParameters.ControlParameters()

    controlParameters.ControlParameterPrefix.setPrefix(identifier)
    controlParameters.ControlParameterCost.setCost(25)

    var encoder = new encoding.Encoder
    try {
        encoder.encoderReset(encoding.MaxPacketSize, 0)
    }catch (err){
        return [null, err]
    }
    try {
        controlParameters.wireEncode(encoder)
    }catch (err){
        return [null, err]
    }
    var controlParametersbuf
    try {
        controlParametersbuf = encoder.getBuffer()
    }catch (err){
        return [null, err]
    }
    // 这里不能再赋值 返回的是数组中的元素个数
    // componentContainer = componentContainer.push(identifiercomponent.createIdentifierComponentByByteArray(controlParametersbuf))
    componentContainer.push(identifiercomponent.createIdentifierComponentByByteArray(controlParametersbuf))
    var commandIdentifier
    try {
        commandIdentifier = createIdentifierByComponents(componentContainer)
    }catch (err){
        console.log(err)
        return [null, err]
    }
    interest.setName(commandIdentifier)
    return [interest, null]
}

module.exports = {
    createRegisterIdentifierInterest: createRegisterIdentifierInterest
}